package mvc;

import frame.Autowired;
import frame.Component;
import org.apache.catalina.Context;
import org.apache.catalina.LifecycleException;
import org.apache.catalina.startup.Tomcat;

import java.io.File;

@Component
public class TomcatServer {

    @Autowired
    private DispatcherServlet dispatcherServlet;

    public void start() throws LifecycleException {
        int port = 8080;
        Tomcat tomcat = new Tomcat();
        tomcat.setPort(8080);
        tomcat.getConnector();

        String contextPath = "";
        String docBase = new File(".").getAbsolutePath();
        Context context = tomcat.addContext(contextPath, docBase);

        tomcat.addServlet(contextPath, "dispatcherServlet", dispatcherServlet);
        context.addServletMappingDecoded("/*","dispatcherServlet");
        tomcat.start();

        System.out.println("Tomcat server started" + port);
    }
}
